import json

from flask import Blueprint, request
from utils.Response import Response

history_bp = Blueprint('history_bp', import_name=__name__, url_prefix='/history')


@history_bp.get("")
def get_friendList():
    ID = request.args.get('ID', '')
    with open("data/latast_userID.txt", 'w', encoding='utf-8') as file:
        file.write(ID)
    fileadress="data/"+str(ID)+".txt"
    try:
        with open(fileadress, 'r', encoding='utf-8') as file:
            conversations = file.read()  # 读取整个文件内容为一个长字符串
            return Response.success(data=json.loads(conversations+"]"))
    except FileNotFoundError:
        conversations=""
        print("查无"+ID+"聊天记录")
        return Response.success(data=json.loads('[{"role": "assistant","content": "哈喽"}]'))